package com.ecube.mqtt.page.listener.swapListener;

import com.ecube.mqtt.mysql.Query;
import com.ecube.mqtt.page.listener.chargelistener.BackListener;
import com.ecube.mqtt.service.SwapSendService;
import com.ecube.mqtt.utils.CommonConstant;
import com.ecube.mqtt.utils.PropertyUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SwapStartResultPageListener implements ActionListener {
    private JComboBox env;
    private JTextField stationId;
    private JTextField vehicletext;


//    private JTextField vehicletext;
//    private JTextField vintext;
    private JFrame start;
    private  String path;
    public SwapStartResultPageListener(JFrame start,
                                       JComboBox env,
                                       JTextField stationId,
                                       JTextField vehicletext,
                                       String path) {
        this.start = start;
        this.env = env;
        this.stationId = stationId;
        this.vehicletext = vehicletext;
        this.path = path;
//        this.vehicletext = vehicletext;
//        this.vintext = vintext;
    }

    public void actionPerformed(ActionEvent e) {
        start.dispose();
        Map propertys = new HashMap();

        propertys.put("env",env.getSelectedItem().toString());
        propertys.put("stationId",stationId.getText());
        propertys.put("vehicle",vehicletext.getText());
        try {
            PropertyUtil.setProperty(propertys, CommonConstant.SWAP_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);
        Font font = new Font("宋体",Font.PLAIN,18);

        //生成新界面
        JFrame startpageres = new JFrame();
        startpageres.setSize(500, 700);
        startpageres.setTitle("启动失败模拟");//设置窗体标题
        startpageres.setDefaultCloseOperation(2);
        startpageres.setLocationRelativeTo(null);
        startpageres.setFont(font);  //宋体，正常风格，14号字体
        //实例化FlowLayout流式布局类的对象，指定对齐方式为居中对齐组件之间的间隔为10个像素
        FlowLayout fl = new FlowLayout(FlowLayout.RIGHT,30,30);
        //实例化流式布局类的对象
        startpageres.setLayout(fl);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel labname = new JLabel("环境：");
        labname.setFont(font);
        //将labname标签添加到窗体上
        startpageres.add(labname);
        startpageres.add(env);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel stationname = new JLabel("站ID：");
        stationname.setFont(font);
        //将labname标签添加到窗体上
        startpageres.add(stationname);
        startpageres.add(stationId);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel vehicle = new JLabel("车牌：");
        vehicle.setFont(font);
        startpageres.add(vehicle);
        startpageres.add(vehicletext);

        //实例化JLabel标签对象，该对象显示“result”
        JLabel resultname = new JLabel("result：");
        resultname.setFont(font);
        //将labname标签添加到窗体上
        startpageres.add(resultname);

        JComboBox result = new JComboBox();
        result.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        result.setFont(font);
        result.addItem("1-成功");
        result.addItem("2-失败");
        result.setSelectedItem("2-失败");
        //env.getSelectedItem().toString()
        startpageres.add(result);


        //实例化JLabel标签对象，该对象显示“result”
        JLabel resonname = new JLabel("reson：");
        resonname.setFont(font);
        //将labname标签添加到窗体上
        startpageres.add(resonname);

        JComboBox reson = new JComboBox();
        reson.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        reson.setFont(font);
        reson.addItem("null");
        reson.addItem("1-站控本地模式");
        reson.addItem("2-换电设备本地模式");
        reson.addItem("3-停车不到位");
        reson.addItem("4-数字标签不一致-");
        reson.addItem("5-车机未连接");
        reson.addItem("6-启动换电失败");
        reson.addItem("7-RFID未读取到");
        reson.addItem("8-无可换电池");
        reson.setSelectedItem("1-站控本地模式");
        //env.getSelectedItem().toString()
        startpageres.add(reson);

        //实例化JButton组件
        JButton startResultButton = new JButton();
        //设置按键的显示内容
        startResultButton.setText("   点击启动   ");
        startResultButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        startResultButton.setSize(dim3);
        startpageres.add(startResultButton);

        SwapStartResultListener cemqStartResult = new SwapStartResultListener(startpageres,
                env,
                stationId,
                vehicletext,
                result,
                reson,
                path);
        startResultButton.addActionListener(cemqStartResult);

        //实例化JButton组件
        JButton chargeReturnFirstPageButton = new JButton();
        //设置按键的显示内容
        chargeReturnFirstPageButton.setText("   返回首页   ");
        chargeReturnFirstPageButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        chargeReturnFirstPageButton.setSize(dim3);
        startpageres.add(chargeReturnFirstPageButton);


        BackListener chargeBack = new BackListener(startpageres, path);
        chargeReturnFirstPageButton.addActionListener(chargeBack);

        startpageres.setResizable(false);
        startpageres.setVisible(true);


    }
}